﻿//----------------------------------------------
// Flip Web Apps: Game Framework
// Copyright © 2016 Flip Web Apps / Mark Hewitt
//
// Please direct any bugs/comments/suggestions to http://www.flipwebapps.com
// 
// The copyright owner grants to the end user a non-exclusive, worldwide, and perpetual license to this Asset
// to integrate only as incorporated and embedded components of electronic games and interactive media and 
// distribute such electronic game and interactive media. End user may modify Assets. End user may otherwise 
// not reproduce, distribute, sublicense, rent, lease or lend the Assets. It is emphasized that the end 
// user shall not be entitled to distribute or transfer in any way (including, without, limitation by way of 
// sublicense) the Assets in any other way than as integrated components of electronic games and interactive media. 

// The above copyright notice and this permission notice must not be removed from any files.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
// THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//----------------------------------------------

using GameFramework.GameObjects.Components.AbstractClasses;
using UnityEngine;
using UnityEngine.Assertions;

namespace GameFramework.FreePrize.Components
{
    /// <summary>
    /// Shows an enabled or a disabled gameobject based upon whether there is a free prize available
    /// </summary>
    [AddComponentMenu("Game Framework/FreePrize/EnableIfPrizeAvailable")]
    [HelpURL("http://www.flipwebapps.com/game-framework/")]
    public class EnableIfPrizeAvailable : RunOnState
    {
        /// <summary>
        /// Optional Gameobject to show when a prize is available.
        /// </summary>
        [Tooltip("Optional Gameobject to show when a prize is available.")]
        public GameObject PrizeAvailableGameObject;

        /// <summary>
        /// Optional Gameobject to show when the countdown is taking place.
        /// </summary>
        [Tooltip("Optional Gameobject to show when the countdown is taking place.")]
        public GameObject PrizeCountdownGameObject;

        /// <summary>
        /// Optional Gameobject to show when waiting for a new countdown to start.
        /// </summary>
        [Tooltip("Optional Gameobject to show when waiting for a new countdown to start.")]
        public GameObject DelayGameObject;

        /// <summary>
        /// Show the correct gameobject based upon the current state.
        /// </summary>
        public override void RunMethod()
        {
            Assert.IsTrue(FreePrizeManager.IsActive, "Please ensure that GameFramework.FreePrize.Components.FreePrizeManager is added to Edit->ProjectSettings->ScriptExecution before 'Default Time'.\n" +
                                                "GameFramework.FreePrize.Components.EnableIfPrizeAvailable does not necessarily need to appear in this list, but if it does ensure FreePrizeManager comes first");

            var isPrizeAvailable = FreePrizeManager.Instance.IsPrizeAvailable();
            var isCountingDown = FreePrizeManager.Instance.IsCountingDown();

            if (PrizeAvailableGameObject != null)
                PrizeAvailableGameObject.SetActive(isPrizeAvailable);

            if (PrizeCountdownGameObject != null)
                PrizeCountdownGameObject.SetActive(isCountingDown);

            if (DelayGameObject != null)
                DelayGameObject.SetActive(!isPrizeAvailable && !isCountingDown);
        }
    }

}